
use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1
append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"
replace treated=1 if d==2
append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_d_distrib_as_treated_20250319.dta"

keep if control==0
cap drop ABS
gen ABS=0 if d==1
replace ABS=1 if d>=2 & d<=6

keep if d<=6

* merge in BNOs *
gen st=tmerge_t
merge 1:1 anon st using "/homeKRTK/health_shocks/bnoSext_code_list.dta"
drop if _m==2
drop _m


gen BP=.
replace 	BP = 	7	if 	BNOS03	==	1
replace 	BP = 	7	if 	BNOS13	==	1
replace 	BP = 	7	if 	BNOS23	==	1
replace 	BP = 	7	if 	BNOS33	==	1
replace 	BP = 	1	if 	BNOS40	==	1
replace 	BP = 	1	if 	BNOS41	==	1
replace 	BP = 	1	if 	BNOS42	==	1
replace 	BP = 	1	if 	BNOS43	==	1
replace 	BP = 	1	if 	BNOS44	==	1
replace 	BP = 	1	if 	BNOS45	==	1
replace 	BP = 	1	if 	BNOS46	==	1
replace 	BP = 	1	if 	BNOS49	==	1
replace 	BP = 	2	if 	BNOS50	==	1
replace 	BP = 	2	if 	BNOS51	==	1
replace 	BP = 	2	if 	BNOS52	==	1
replace 	BP = 	2	if 	BNOS53	==	1
replace 	BP = 	2	if 	BNOS54	==	1
replace 	BP = 	2	if 	BNOS55	==	1
replace 	BP = 	2	if 	BNOS56	==	1
replace 	BP = 	2	if 	BNOS59	==	1
replace 	BP = 	3	if 	BNOS60	==	1
replace 	BP = 	3	if 	BNOS61	==	1
replace 	BP = 	3	if 	BNOS62	==	1
replace 	BP = 	3	if 	BNOS63	==	1
replace 	BP = 	3	if 	BNOS64	==	1
replace 	BP = 	3	if 	BNOS65	==	1
replace 	BP = 	3	if 	BNOS66	==	1
replace 	BP = 	3	if 	BNOS69	==	1
replace 	BP = 	4	if 	BNOS70	==	1
replace 	BP = 	4	if 	BNOS71	==	1
replace 	BP = 	4	if 	BNOS72	==	1
replace 	BP = 	4	if 	BNOS73	==	1
replace 	BP = 	4	if 	BNOS74	==	1
replace 	BP = 	4	if 	BNOS75	==	1
replace 	BP = 	4	if 	BNOS76	==	1
replace 	BP = 	4	if 	BNOS79	==	1
replace 	BP = 	5	if 	BNOS80	==	1
replace 	BP = 	5	if 	BNOS81	==	1
replace 	BP = 	5	if 	BNOS82	==	1
replace 	BP = 	5	if 	BNOS83	==	1
replace 	BP = 	5	if 	BNOS84	==	1
replace 	BP = 	5	if 	BNOS85	==	1
replace 	BP = 	5	if 	BNOS86	==	1
replace 	BP = 	5	if 	BNOS89	==	1
replace 	BP = 	6	if 	BNOS90	==	1
replace 	BP = 	6	if 	BNOS91	==	1
replace 	BP = 	6	if 	BNOS92	==	1
replace 	BP = 	6	if 	BNOS93	==	1
replace 	BP = 	6	if 	BNOS94	==	1
replace 	BP = 	6	if 	BNOS95	==	1
replace 	BP = 	6	if 	BNOS96	==	1
replace 	BP = 	6	if 	BNOS99	==	1

gen acc_type=.
replace 	acc_type=	4	if 	BNOS03	==	1
replace 	acc_type=	4	if 	BNOS13	==	1
replace 	acc_type=	4	if 	BNOS23	==	1
replace 	acc_type=	4	if 	BNOS33	==	1
replace 	acc_type=	1	if 	BNOS40	==	1
replace 	acc_type=	2	if 	BNOS41	==	1
replace 	acc_type=	3	if 	BNOS42	==	1
replace 	acc_type=	4	if 	BNOS43	==	1
replace 	acc_type=	1	if 	BNOS44	==	1
replace 	acc_type=	1	if 	BNOS45	==	1
replace 	acc_type=	1	if 	BNOS46	==	1
replace 	acc_type=	1	if 	BNOS49	==	1
replace 	acc_type=	1	if 	BNOS50	==	1
replace 	acc_type=	2	if 	BNOS51	==	1
replace 	acc_type=	3	if 	BNOS52	==	1
replace 	acc_type=	4	if 	BNOS53	==	1
replace 	acc_type=	1	if 	BNOS54	==	1
replace 	acc_type=	1	if 	BNOS55	==	1
replace 	acc_type=	1	if 	BNOS56	==	1
replace 	acc_type=	1	if 	BNOS59	==	1
replace 	acc_type=	1	if 	BNOS60	==	1
replace 	acc_type=	2	if 	BNOS61	==	1
replace 	acc_type=	3	if 	BNOS62	==	1
replace 	acc_type=	4	if 	BNOS63	==	1
replace 	acc_type=	1	if 	BNOS64	==	1
replace 	acc_type=	1	if 	BNOS65	==	1
replace 	acc_type=	1	if 	BNOS66	==	1
replace 	acc_type=	1	if 	BNOS69	==	1
replace 	acc_type=	1	if 	BNOS70	==	1
replace 	acc_type=	2	if 	BNOS71	==	1
replace 	acc_type=	3	if 	BNOS72	==	1
replace 	acc_type=	4	if 	BNOS73	==	1
replace 	acc_type=	1	if 	BNOS74	==	1
replace 	acc_type=	1	if 	BNOS75	==	1
replace 	acc_type=	1	if 	BNOS76	==	1
replace 	acc_type=	1	if 	BNOS79	==	1
replace 	acc_type=	1	if 	BNOS80	==	1
replace 	acc_type=	2	if 	BNOS81	==	1
replace 	acc_type=	3	if 	BNOS82	==	1
replace 	acc_type=	4	if 	BNOS83	==	1
replace 	acc_type=	1	if 	BNOS84	==	1
replace 	acc_type=	1	if 	BNOS85	==	1
replace 	acc_type=	1	if 	BNOS86	==	1
replace 	acc_type=	1	if 	BNOS89	==	1
replace 	acc_type=	1	if 	BNOS90	==	1
replace 	acc_type=	2	if 	BNOS91	==	1
replace 	acc_type=	3	if 	BNOS92	==	1
replace 	acc_type=	4	if 	BNOS93	==	1
replace 	acc_type=	1	if 	BNOS94	==	1
replace 	acc_type=	1	if 	BNOS95	==	1
replace 	acc_type=	1	if 	BNOS96	==	1
replace 	acc_type=	1	if 	BNOS99	==	1
	

recode teaor1_h1 (-1=99) (.=100)

cap estimates clear

* REGRESSIONS *
global ct_ind 		i.kor i.occupation i.teaor1_h1 ferfi mean_lag_w	// individual-level control variables (non-health) //
global ct_health 	log_health_12mo A_12mo-V_12mo			// individual-level control variables (health) //
global ct_FEi		FEi						// raw AKM individual FE //
global ct_FEi_imp	FEi_imp flag_missingFEi				// imputed AKM individual FE and its missing flag //
global ct_firm 		size_pre_imp FO_pre_imp logmean_w_pre_imp flag_missingsize_pre flag_missingFO_pre flag_missinglogmean_w_pre 	// firm-level control variables, corresponding to firm at t //
global ct_FEj		FEj_l4 FEj_l5 FEj_l6 FEj_l7 FEj_l8 FEj_l9 FEj_l10 FEj_l11 FEj_l12 					// raw AKM firm FE //
global ct_FEj_imp	flag_missingFEj_l4-flag_missingFEj_l12 FEj_l4_imp - FEj_l12_imp 		// imputed AKM firm FE and its missing flag //
global FE		i.d i.t						// d and time FE //
global FEt		i.t						// d and time FE //
global FEd		i.d						// d and time FE //

reg ABS i.t i.acc_type##i.BP $ct_ind $ct_health $ct_FEi_imp $ct_firm $ct_FEj_imp , cluster(t) // 19.6% is the R-squared // 

***Merge info on occupation of job upon shock

merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_feor.dta", nogen keep(master match) keepusing(feor1_2003_4 feor1_2008_4)

rename feor1_2003_4 feor93
rename feor1_2008_4 feor08

merge m:1 feor93 using "/homeKRTK/health_shocks/ONET/onet_indices_feor93.dta", nogen keep(master match) keepusing(std_physical_narrow std_remote_narrow std_respons_narrow std_others_narrow std_stress_narrow)
foreach var of varlist std_physical_narrow std_remote_narrow std_respons_narrow std_others_narrow std_stress_narrow {
	rename `var' `var'_93
}

merge m:1 feor08 using "/homeKRTK/health_shocks/ONET/onet_indices_feor08.dta", nogen keep(master match) keepusing(std_physical_narrow std_remote_narrow std_respons_narrow std_others_narrow std_stress_narrow)
foreach var of varlist std_physical_narrow std_remote_narrow std_respons_narrow std_others_narrow std_stress_narrow {
	replace `var'=`var'_93 if `var'==. & `var'_93~=.
	drop `var'_93
	rename `var' `var'_t0
}


cap drop sens1 spec1 spec_1 
logit ABS i.t i.kor ferfi
lsens, gensens(sens1) genspec(spec1) replace nograph
lroc //Area under ROC curve   =   0.5685
gen spec_1 = 1-spec1
cap drop spec_1r 
cap drop sens1r
gen spec_1r=round(spec_1*100)/100
by spec_1r, sort: egen sens1r=mean(sens1)

cap drop sens2 spec2 spec_2
logit ABS i.t i.kor ferfi i.acc_type##i.BP
lsens, gensens(sens2) genspec(spec2) replace nograph
lroc //Area under ROC curve   =   0.6369
gen spec_2 = 1-spec2
cap drop spec_2r sens2r
gen spec_2r=round(spec_2*100)/100
by spec_2r, sort: egen sens2r=mean(sens2)

cap drop sens3 spec3 spec_3
logit ABS i.t i.kor ferfi i.acc_type##i.BP $ct_ind $ct_health $ct_FEi_imp $ct_firm $ct_FEj_imp
lsens, gensens(sens3) genspec(spec3) replace nograph
lroc //Area under ROC curve   =   0.7579
gen spec_3 = 1-spec3
cap drop spec_3r sens3r
gen spec_3r=round(spec_3*100)/100
by spec_3r, sort: egen sens3r=mean(sens3)

//the "ferfi ferfi" line simply draws the 45 degree reference line

twoway (line ferfi ferfi, lcolor(gs10))  (connected sens1r spec_1r , sort msize(vsmall) msymbol(circle) mcolor(black) lcolor(black))  (connected sens2r spec_2r , sort msymbol(X) mcolor(blue) lcolor(blue))  (connected sens3r spec_3r , msize(medium) msymbol(oh) mcolor(red) lcolor(red)) , graphregion(color(white)) bgcolor(white) legend(on order(2 "Age & gender (AUC 0.57)" 3 "Age & gender & shock type (AUC 0.64)" 4 "Full set of predictors (AUC 0.76)" ) region(lwidth(none))  col(1) pos(6)) xtitle("False positive rate (1-specificity)") ytitle("True positive rate (sensitivity)") xlab(0(.25)1) ylab(0(.25)1) aspect(1) plotregion(margin(zero)) 

graph export "/home/transfer/ABS_ROC.pdf", as(pdf) replace
graph export "/home/transfer/ABS_ROC.eps", as(eps) replace
